package com.atguigu.paymentdemo.controller;

import com.atguigu.paymentdemo.bo.OssUploadResult;
import com.atguigu.paymentdemo.util.AjaxResult;
import com.atguigu.paymentdemo.util.AliyunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author wulifeng
 */
@Api(tags = "通用Controller")
@RestController
@RequestMapping("common")
@AllArgsConstructor
public class CommonController {
    /**
     * 上传文件
     *
     * @param file 文件
     * @return OssUrl
     */
    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/upload", produces = {"application/json;charset=UTF-8"})
    public AjaxResult upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String ext = fileName.substring(fileName.indexOf(".")+1);
        OssUploadResult rs = AliyunOssUtil.upload(file.getBytes(),ext);
        rs.setFileName(file.getOriginalFilename());
        return AjaxResult.success("上传成功", rs);
    }
}
